﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Linq;
using EOSCRM.Domain;
using EOSCRM.Util;


namespace EOSCRM.Dao
{
    public  class ThayDoiTinhTrangDao : BaseDao
    {  
        public THAYDOITINHTRANG Get(string macay)
        {
            return _db.THAYDOITINHTRANGs.Where(p => p.MaCay.Equals(macay)).OrderByDescending(p=>p.NgayGhiNhan).FirstOrDefault();
        }

        public List<THAYDOITINHTRANG> Search(string key)
        {
            return _db.THAYDOITINHTRANGs.Where(p => p.MaNV.ToUpper().Contains(key.ToUpper())).ToList();
        }

        public List<THAYDOITINHTRANG> GetList()
        {
            return _db.THAYDOITINHTRANGs.ToList();
        }

        public List<THAYDOITINHTRANG> GetList(int fromIndex, int toIndex)
        {
            return GetList().Skip(fromIndex).Take(toIndex - fromIndex).ToList();
        }

        public int Count()
        {
            return _db.THAYDOITINHTRANGs.Count();
        }

        public Message Insert(THAYDOITINHTRANG objUi)
        {
            Message msg;
            try
            {

                _db.Connection.Open();

                _db.THAYDOITINHTRANGs.InsertOnSubmit(objUi);
                _db.SubmitChanges();

                // commit

                // success message
                msg = new Message(MessageConstants.I_CREATE_SUCCEED, MessageType.Info, "thay đổi tình trạng cây");
            }
            catch (Exception ex)
            {
                // rollback transaction

                msg = ExceptionHandler.HandleInsertException(ex, "thay đổi tình trạng cây", objUi.MaMKN);
            }
            return msg;
        }

        
        public bool IsInUse(string ma)
        {
            //if (_db.DUONGPHOs.Where(p => p.MaMKN.Equals(ma)).Count() > 0)
            //    return true;

            //if (_db.PHUONGs.Where(p => p.MaMKN.Equals(ma)).Count() > 0)
            //    return true;

            //if (_db.DONDANGKies.Where(p => p.MaMKN .Equals(ma)).Count() > 0)
            //    return true;

            //if (_db.NHANVIENs.Where(p => p.MaMKN .Equals(ma)).Count() > 0)
            //    return true;

            //if (_db.TIEUTHUs  .Where(p => p.MaMKN.Equals(ma)).Count() > 0)
            //    return true;

            //if (_db.KHACHHANGs .Where(p => p.MaMKN.Equals(ma)).Count() > 0)
            //    return true;

            //if (_db.GIAIQUYETTHONGTINSUACHUAs .Where(p => p.MaMKN.Equals(ma)).Count() > 0)
            //    return true;

            //if (_db.HOPDONGs .Where(p => p.MaMKN.Equals(ma)).Count() > 0)
            //    return true;

            return false;
        }
    }
}
